package com.javajun.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.javajun.custom.BaseService;
import com.javajun.entity.MainGrade;
import com.javajun.repository.MainGradeRepository;

@Service
public class MainGradeService extends BaseService<MainGrade, Integer>{

	@Autowired
	private MainGradeRepository mainGradeRepository;
	
	public Specification<MainGrade> BuildSpec(MainGrade entity) {
		Specification<MainGrade> spec = new Specification<MainGrade>() {

			@Override
			public Predicate toPredicate(Root<MainGrade> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				predicates.add(cb.greaterThan(root.get("id"), 0));
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		return spec;
	}
	
	public List<MainGrade> FindAllByOrderByStduentId() {
		return mainGradeRepository.findAllByOrderByStudentId();
	}
}
